package com.tms.user.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tms.systemPermission.bean.SystemPermission;
import com.tms.user.bean.User;
import com.tms.user.dto.UserLoginByUserNameDTO;
import com.tms.user.exception.BusinessException;
import com.tms.user.feign.PermissionFeignClient;
import com.tms.user.mapper.UserMapper;
import com.tms.user.service.UserService;
import com.tms.user.utils.MD5Util;
import com.tms.user.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Author：潘建冰
 * Package：com.tms.user.service.impl
 * Project：TaskManageSystem
 * name：UserServiceImpl
 * Date：2025/3/24  20:51
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    PermissionFeignClient permissionFeignClient;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public Map<String, Object> loginByUserName(UserLoginByUserNameDTO userLoginByUserNameDTO) {
        String username = userLoginByUserNameDTO.getUsername();
        String password = userLoginByUserNameDTO.getPassword();
        //查询用户信息
        User user = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username)
                .eq(User::getStatus,1).eq(User::getIsDeleted,0));
        if (user == null){
            throw new BusinessException("没有该用户");
        }
        //比对密码
        boolean match = MD5Util.match(password, user.getPassword());
        if (match){
            //根据用户id登录
            StpUtil.login(user.getId());
            // 获取Token相关参数
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            //获取菜单树
            List<SystemPermission> menuTree = permissionFeignClient.getMenuTree(user.getId());
            //把权限码和角色码存储到redis中
            setPermissionOnRedis(user.getId());
            //返回前端
            return Map.of(
                    "token",tokenInfo,
                    "userInfo",user,
                    "menuTree",menuTree
            );
        }
        throw new BusinessException("密码错误");
    }

    //把权限码和角色码存储到redis中
    //TODO 迁移到权限模块执行redis存储
    private void setPermissionOnRedis(String id) {
        //feign 调用获得权限码集合
        List<String> permissionCode = permissionFeignClient.getPermissionCode(id);
        redisUtil.delete("permission:permissionCode:user:"+id);
        redisUtil.lRightPushAll("permission:permissionCode:user:"+id,permissionCode);
        //feign 调用获得角色码集合
        List<String> roleCode = permissionFeignClient.getRoleCode(id);
        redisUtil.delete("permission:roleCode:user:"+id);
        redisUtil.lRightPushAll("permission:roleCode:user:"+id,roleCode);
    }
}
